#include "typeface_getter.h"

#include <include/core/SkFontMgr.h>

void TypefaceGetter::GetByFamilies(const std::string &families, const SkFontStyle &style, std::vector<Typeface> &tfs)
{
    auto font = SkTypeface::MakeFromFile("out/seguiemj.ttf");
    if (font) {
        tfs.emplace_back(font);
    }

    font = SkTypeface::MakeFromFile("out/NotoSansArabic-Regular.ttf");
    if (font) {
        tfs.emplace_back(font);
    }

    auto fm = SkFontMgr::RefDefault();
    std::vector<int> splitedIndexs;
    {
        for (auto i = 0; i != std::string::npos && i < families.size(); i = families.find(',', i + 1)) {
            splitedIndexs.push_back(i);
        }
        splitedIndexs.push_back(families.size());
    }
    auto start = 0;
    for (auto i : splitedIndexs) {
        if (i > start) {
            auto familyName = families.substr(start, i - start);
            auto typeface = fm->matchFamilyStyle(familyName.c_str(), style);
            if (typeface) {
                tfs.emplace_back(typeface);
            }
            start = i + 1;
        }
    }
}
